package com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.util.CacheUtil;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.SmsUseMoneyApplicationProcess;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.dto.SmsUseMoneyApplicationProcessDTO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.entity.vo.SmsUseMoneyApplicationProcessVO;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.mapper.SmsUseMoneyApplicationProcessMapper;
import com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.service.SmsUseMoneyApplicationProcessService;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsRole;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsRoleMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.constant.useMoneyObjectTypeConstant.USE_MONEY_APPLICATION_OBJECT_TYPE_PERSON;
import static com.xyht.sca_s.student_manage_system.modules.useMoneyApplication.constant.useMoneyObjectTypeConstant.USE_MONEY_APPLICATION_OBJECT_TYPE_ROLE;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2023-05-14
 */
@Service
public class SmsUseMoneyApplicationProcessServiceImpl extends ServiceImpl<SmsUseMoneyApplicationProcessMapper, SmsUseMoneyApplicationProcess> implements SmsUseMoneyApplicationProcessService {
    @Autowired
    SmsUseMoneyApplicationProcessMapper smsUseMoneyApplicationProcessMapper;
    @Autowired
    private CacheUtil cacheUtil;
    @Autowired
    private SmsRoleMapper smsRoleMapper;
    @Override
    public ResponseResult updateSmsUseMoneyApplicationProcess(SmsUseMoneyApplicationProcessVO processVO) {
        if(isNullOrEmpty(processVO.getId())){
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if (isNullOrEmpty(processVO.getCheckType())) {//如果流程对象类型不是空
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }
        if(processVO.getProcessIsApplicationEdit()&&processVO.getCheckType()==USE_MONEY_APPLICATION_OBJECT_TYPE_ROLE){
            if(isNullOrEmpty(processVO.getCheckId())){
                return CommonResult.failed(CommonCodeEnum.USE_MONEY_PROCESS_NOT_TYPE);
            }
        }
        SmsUseMoneyApplicationProcess process =
                smsUseMoneyApplicationProcessMapper.selectById(processVO.getId());
        if (process == null || isNullOrEmpty(process.getId())) {
            return CommonResult.failed(CommonCodeEnum.USE_MONEY_PROCESS_NOT_EXIST);
        }
        if (processVO.getCheckType() != null && !isNullOrEmpty(processVO.getCheckId())) {
            if (processVO.getCheckType() == USE_MONEY_APPLICATION_OBJECT_TYPE_PERSON
            &&!processVO.getProcessIsApplicationEdit()) {
                SmsUser smsUser = cacheUtil.getUserInfo(processVO.getCheckId());
                if (smsUser == null || isNullOrEmpty(smsUser.getId())) {
                    return CommonResult.failed(CommonCodeEnum.USE_MONEY_PROCESS_USER_NOT_EXIST);
                }
            } else if (processVO.getCheckType() == USE_MONEY_APPLICATION_OBJECT_TYPE_ROLE) {
                SmsRole smsRole = smsRoleMapper.selectById(processVO.getCheckId());
                if (smsRole == null || isNullOrEmpty(smsRole.getId())) {
                    return CommonResult.failed(CommonCodeEnum.USE_MONEY_PROCESS_ROLE_NOT_EXIST);
                }
            }
        }
        SmsUseMoneyApplicationProcess updateProcess = new SmsUseMoneyApplicationProcess();
        updateProcess.setId(processVO.getId());
        if (!isNullOrEmpty(processVO.getCheckType())) {
            updateProcess.setCheckId(processVO.getCheckId());
            updateProcess.setCheckType(processVO.getCheckType());
        }
        if (!isNullOrEmpty(processVO.getProcessIsApplicationEdit())) {
            updateProcess.setProcessIsApplicationEdit(processVO.getProcessIsApplicationEdit());
        }
        if (!isNullOrEmpty(processVO.getProcessIsSign())) {
            updateProcess.setProcessIsSign(processVO.getProcessIsSign());
        }
        if (!isNullOrEmpty(processVO.getProcessIsStamp())) {
            updateProcess.setProcessIsStamp(processVO.getProcessIsStamp());
        }
        if (!isNullOrEmpty(processVO.getIsUse())) {
            updateProcess.setIsUse(processVO.getIsUse());
        }

        smsUseMoneyApplicationProcessMapper.updateById(updateProcess);
        return CommonResult.success();
    }

    @Override
    public ResponseResult getUseMoneyApplicationProcessList() {
        List<SmsUseMoneyApplicationProcessDTO> respList =
                smsUseMoneyApplicationProcessMapper.selectList(new QueryWrapper<SmsUseMoneyApplicationProcess>()
                        .lambda()
                        .orderByAsc(SmsUseMoneyApplicationProcess::getSort))
                        .stream()
                        .map(process -> {
                            SmsUseMoneyApplicationProcessDTO processDTO =
                                    new SmsUseMoneyApplicationProcessDTO();
                            BeanUtils.copyProperties(process, processDTO);

                            if (process.getCheckId() != null && !isNullOrEmpty(process.getCheckId())) {
                                if (process.getCheckType() == USE_MONEY_APPLICATION_OBJECT_TYPE_PERSON) {
                                    SmsUser smsUser = cacheUtil.getUserInfo(process.getCheckId());
                                    if (smsUser != null && !isNullOrEmpty(smsUser.getId())) {
                                        processDTO.setProcessObjectName(smsUser.getRealName());
                                        processDTO.setHeadPic(smsUser.getUserPic());
                                    }
                                } else {
                                    SmsRole smsRole = smsRoleMapper.selectById(process.getCheckId());
                                    if (smsRole != null && !isNullOrEmpty(smsRole.getId())) {
                                        processDTO.setProcessObjectName(smsRole.getRoleName());
                                    }
                                }
                            }
                            return processDTO;
                        })
                        .collect(Collectors.toList());
        return CommonResult.success(respList);
    }

}
